package com.ficv.tucanalmovil.dominio;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


import com.ficv.tucanalmovil.web.beans.*;

import javax.persistence.CascadeType;
import javax.persistence.EntityManager;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findPeoplesByNickEquals", "findPeoplesByDeviceIdEquals", "findPeoplesByActivityEqualsAndModeEquals", "findPeoplesByActivityEquals", "findPeoplesByHoraBetween", "findPeoplesByActivityAndHoraBetween", "findPeoplesByActivityEqualsAndModeEqualsAndHoraBetween", "findPeoplesByActivityEqualsAndModeEqualsAndPoblacionEqualsAndHoraGreaterThan", "findPeoplesByOnlineGreaterThan", "findPeoplesByActivityEqualsAndModeEqualsAndOnlineGreaterThan" })
public class People {

    private String nick;

    private String comment;

    private String activity;

    private String color;

    private String abbr;

    private Double lat;

    private Double lon;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date online;

    private String latency;

    private String deviceId;

    private String mode;

    private String refresh;

    private String gpsPrecision;

    private String codigoPostal;

    private String poblacion;

    private String provincia;

    private String calle;

    private String barrio;

    private String distrito;

    private String region;

    private String pais;

    private String telefono;
    
    private Double centerLat;   
    private Double centerLon;    
    private Integer radioKm;    
    
    private String photoUrl;
    
    private Boolean gpsStatus;
    
    @JsonIgnore
    @OneToMany(cascade = CascadeType.ALL)
    private Set<Positions> positions = new HashSet<Positions>();

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date hora;

    private Boolean peticiones;

    public static TypedQuery<com.ficv.tucanalmovil.dominio.People> findPeople(String activity, String mode, Date minHora, People me, double x1, double x2, double y1, double y2) {
        EntityManager em = People.entityManager();
        TypedQuery<People> q = em.createQuery("SELECT o FROM People AS o  WHERE o.activity = :activity  AND o.mode = :mode  AND o.online > :minHora AND " +
        					"NOT EXISTS (SELECT c FROM Contactos AS c WHERE c.blockedAtoB > 1 AND c.contactoA=o AND c.contactoB=:me  OR c.blockedBtoA > 1 AND c.contactoB=o AND c.contactoA=:me) "  +
        					"AND o.lat < :y2 AND o.lat > :y1 AND o.lon < :x2 AND o.lon > :x1 ORDER BY o.online DESC", People.class);
        q.setParameter("activity", activity);
        q.setParameter("mode", mode);
        q.setParameter("minHora", minHora);
        q.setParameter("me", me);        
        q.setParameter("x1", x1);
        q.setParameter("x2", x2);
        q.setParameter("y1", y1);
        q.setParameter("y2", y2);
        return q;
    }
    
    public static List<ActivitiesCount> countActivities(String mode, Date minHora, People me, double x1, double x2, double y1, double y2) {
        EntityManager em = People.entityManager();
        TypedQuery<Object[]> q = em.createQuery("SELECT o.activity, count(o.activity) FROM People AS o  WHERE o.activity NOT LIKE '__event%' AND o.activity NOT LIKE '__activity%' AND o.mode = :mode  AND o.online > :minHora AND " +
        					"NOT EXISTS (SELECT c FROM Contactos AS c WHERE c.blockedAtoB > 1 AND c.contactoA=o AND c.contactoB=:me  OR c.blockedBtoA > 1 AND c.contactoB=o AND c.contactoA=:me) "  +
        					"AND o.lat < :y2 AND o.lat > :y1 AND o.lon < :x2 AND o.lon > :x1  GROUP BY o.activity ORDER BY o.online DESC", Object[].class);
        //q.setParameter("activity", activity);
        q.setParameter("mode", mode);
        q.setParameter("minHora", minHora);
        q.setParameter("me", me);        
        q.setParameter("x1", x1);
        q.setParameter("x2", x2);
        q.setParameter("y1", y1);
        q.setParameter("y2", y2);
        
        
        List<ActivitiesCount> l = new ArrayList<ActivitiesCount>();
        for(Object[] obj : q.getResultList()) {
        	ActivitiesCount a = new ActivitiesCount();
        	a.setActivity((String)obj[0]);
        	a.setCount((Long)obj[1]);
        	l.add(a);
        }
        
        return l;
    }    
}
